﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace FsAddInManager
{
    public static class FsAppBase
    {
        #region 配置文件路径

        private static string _dllDir = null; //dll文件所在路径
        private static string _dllParent = null; //dll文件所在路径的父路径
        private static readonly string m_strDefaultInifilePath = AppDllDir + "\\FsAddinSetting" + "\\FsAddinSetting.ini";
        private static readonly string m_strDefaultIniDirPath = AppDllDir + "\\FsAddinSetting";


        /// <summary>
        ///当前"应用"dll所在目录,结尾不包括"\\"
        /// </summary>
        /// <returns></returns>
        public static string AppDllDir
        {
            get
            {
                if (!String.IsNullOrEmpty(_dllDir))
                {
                    return _dllDir;
                }

                string location = Assembly.GetExecutingAssembly().Location; //dll全路径
                _dllDir = System.IO.Path.GetDirectoryName(location); //返回父亲路径,结尾不包括"\\"

                return _dllDir;
            }
        }

        /// <summary>
        ///当前"应用"dll所在目录的父目录,结尾不包括"\\"
        /// </summary>
        /// <returns></returns>
        public static string AppParentDir
        {
            get
            {
                if (!String.IsNullOrEmpty(_dllParent))
                {
                    return _dllParent;
                }

                try
                {
                    DirectoryInfo dr1 = new DirectoryInfo(AppDllDir);
                    Debug.Assert(dr1 != null);
                    DirectoryInfo dr2 = dr1.Parent;
                    Debug.Assert(dr2 != null, nameof(dr2) + " != null");
                    _dllParent = dr2.FullName; //返回父亲路径

                    return _dllParent;
                }
                catch (Exception e)
                {
                    return "Error";
                }
            }
        }

        /// <summary>
        /// 默认ini文件的路径
        /// </summary>
        public static string DefaultIniPath
        {
            get
            {
                if (!Directory.Exists(m_strDefaultIniDirPath))
                {
                    Directory.CreateDirectory(m_strDefaultIniDirPath); //目标目录下不存在此文件夹即创建子文件夹
                }

                if (!File.Exists(m_strDefaultInifilePath))
                {
                    File.Create(m_strDefaultInifilePath);
                }

                return m_strDefaultInifilePath;
            }
        }

        /// <summary>
        /// 默认ini文件夹的路径
        /// </summary>
        public static string DefaultDirPath
        {
            get
            {
                if (!Directory.Exists(m_strDefaultIniDirPath))
                {
                    Directory.CreateDirectory(m_strDefaultIniDirPath); //目标目录下不存在此文件夹即创建子文件夹
                }

                return m_strDefaultIniDirPath;
            }
        }

        #endregion
    }
}
